package com.ruyue.csinstall.utils;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class Utils {
    public static final String EMPTY = "";

    public static String trimToEmpty(String str) {
        return str == null ? EMPTY : str.trim();
    }

    public static boolean isEmpty(String str) {
        if(str == null || str.trim().length() == 0) {
            return true;
        }
        return false;
    }
    public static boolean isNotEmpty(String str) {
        if(str == null || str.trim().length() == 0) {
            return false;
        }
        return true;
    }
    
    public static String getDateTimeNow() {
        return getDateTimeNow("yyyyMMddHHmmss");
    }
    
    public static String getDateTimeNow(String format) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
        GregorianCalendar cal = new GregorianCalendar();
        return bartDateFormat.format(cal.getTime());
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr) {
        return isEmpty(str) ? defaultStr : str;
    }


    public static String generatePkgId() {
        return UUID.randomUUID().toString();
    }

    public static String getPercent(int num1, int num2){
        NumberFormat numberFormat = NumberFormat.getInstance();
        numberFormat.setMaximumFractionDigits(0);
        return numberFormat.format((float) num1 / (float) num2 * 100) + "%";
    }

    public static void sleep(int timeinterval){
        try {
            TimeUnit.MILLISECONDS.sleep(timeinterval);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}









